package org.KeyCrafter.key;

import org.KeyCrafter.cmd.DBCommand;
import org.KeyCrafter.cmd.ServerCommand;
import org.KeyCrafter.server.ServerContext;
import org.KeyCrafter.server.aof.AofAttribute;
import org.KeyCrafter.server.session.ClientSession;
import org.KeyCrafter.util.SharedReply;

import static org.KeyCrafter.server.AppendOnlyFile.rewriteAppendOnlyFileBackground;
import static org.KeyCrafter.server.ServerConstants.KeyCrafter_OK;
import static org.KeyCrafter.util.ReplyUtil.*;

public class GenericKey implements DBCommand, ServerCommand {
    private static final ServerContext sc = ServerContext.DEFAULT;
    @Override
    public void delCommand(ClientSession c) {
        c.db().delCommand(c);
    }

    @Override
    public void selectCommand(ClientSession c) {
        c.db().selectCommand(c);
    }

    @Override
    public void bgrewriteaofCommand(ClientSession c) {
        AofAttribute aof = sc.getAOFAttribute();
        // 不能重复运行 BGREWRITEAOF
        if (aof.getRewriteThreadId() != -1) {
            addReplyError(c,"Background append only file rewriting already in progress");
        }
        else if ( rewriteAppendOnlyFileBackground() == KeyCrafter_OK) {
            // 执行 BGREWRITEAOF
            addReplySimpleString(c,"Background append only file rewriting started");
        } else {
            addReply(c, SharedReply.err);
        }
    }
}
